using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIEquipDecompose : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIEquipDecompose
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public UnityEngine.UI.ExLoopHorizontalScrollRect loopHorizontalScrollRect { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv0);
                this.btnCancel = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv1);
                this.btnConfirm = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("loopHorizontalScrollRect", out var __tbv2);
                this.loopHorizontalScrollRect = __tbv2;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIEquipDecompose),
            layer = EUILayer.Pop,
            activeBlurBg = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config


        #region fields & properties
        private LoopScrollAdapter<UIN_OneItem> rewards;
        private Dictionary<int, int> dicRewardCidNum = null;
        private List<int> rewardCids = null;

        protected UIB_UIEquipDecompose ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        private UD_UIEquipDecompose uiData => _data as UD_UIEquipDecompose;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIEquipDecompose();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.rewards = this.InitLoopScroll<UIN_OneItem>(this.ui.loopHorizontalScrollRect, this.OnItemChanged);
            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
            this.ui.btnCancel.onClick.AddListener(this.OnClickCancel);
        }

        protected override void OnShow(UIBaseData data)
        {
            UD_UIEquipDecompose d = data as UD_UIEquipDecompose;
            this.RefreshData(d.idList);
            this.View();
        }

        protected override void AddUGUIListener()
        {

        }

        protected override void AddMsgListeners()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.rewards = null;

            this.ClearData();
        }

        #region methonds

        #region 单击
        void OnClickCancel()
        {
            this.Close();
        }

        void OnClickConfirm()
        {
            this.SendDecompose().Forget();
        }

        async UniTaskVoid SendDecompose()
        {
            var result = await EquipmentModule.NetCore.CS_decompose(this.uiData.idList);
            if (result.IsSuccess)
            {
                NoticeModule.ShowUIItemGet(result.awardPods);
                MsgDispatcher.Broadcast(MsgEventType.EquipDecomposeSuccess);
                this.Close();
            }
        }
        #endregion

        void ClearData()
        {
            if (this.rewardCids != null)
                ListPool<int>.Put(this.rewardCids);
            this.rewardCids = null;

            if (this.dicRewardCidNum != null)
                DictionaryPool<int, int>.Put(this.dicRewardCidNum);
            this.dicRewardCidNum = null;
        }

        void RefreshData(List<long> equipIds)
        {
            this.ClearData();

            this.rewardCids = ListPool<int>.Get();
            this.dicRewardCidNum = DictionaryPool<int, int>.Get();
            EquipmentModule.Instance.CalcDecomposeCosts(equipIds, ref this.dicRewardCidNum);
            this.rewardCids.AddRange(this.dicRewardCidNum.Keys);
        }

        void View()
        {
            this.ViewRewards();
        }

        void ViewRewards()
        {
            this.rewards.RefillCells(this.rewardCids.Count);
        }

        void OnItemChanged(UIN_OneItem item, int index)
        {
            int cid = this.rewardCids[index];
            var cfg = TableCenter.item.Get(cid);
            if (item.Show(cfg))
            {
                item.SetCount(this.dicRewardCidNum[cid], true);
            }
        }

        #endregion methonds
    }
}
